#include "e2d/core/trans/LayoutGrid.h"
#include "e2d/core/trans/TransformNode.h"
#include "e2d/core/Entity.h"

void e2d::LayoutGrid::OnUpdate()
{
}

void e2d::LayoutGrid::OnUpdateRuntime()
{
}

void e2d::LayoutGrid::UpdateGridChildren()
{
	for (auto e : m_Entity->GetComponentChildren<e2d::Grid>())
	{
		UpdateGridChild(e);
	};
}

void e2d::LayoutGrid::UpdateGridChild(e2d::Entity* e)
{
	const auto& grid = e->Get<e2d::Grid>();
	auto& transformNode = e->Get<e2d::TransformNode>();
	UpdateGridChildTransform(grid, transformNode.m_Local);

}

void e2d::LayoutGrid::UpdateGridChildTransform(const e2d::Grid& grid, e2d::Transform& inst)
{
	
	inst.m_Scale.x = m_SepSize.x * grid.m_NumGrids.x;
	inst.m_Scale.y = m_SepSize.y * grid.m_NumGrids.y;
	inst.m_Scale.z = m_SepSize.z * grid.m_NumGrids.z;
	inst.m_Position.x = (1.f + 2.f * grid.m_GridIndex.x) * (float(m_SepSize.x));
	inst.m_Position.y = (1.f + 2.f * grid.m_GridIndex.y) * (float(m_SepSize.y));
	inst.m_Position.z = (1.f + 2.f * grid.m_GridIndex.z) * (float(m_SepSize.z));

}
